--exec sp_AI_LoadPOforEmail '12/01/2012','02/07/2013','O'
alter proc sp_AI_LoadPOforEmail
@FromDate date,
@ToDate date,
@OpenOnly nvarchar(1) --C/O
as
declare @emaill nvarchar(200)
set @emaill=''
select @emaill=@emaill+email+',' from OHEM where isnull(U_EMAILPO_CC,'N')='Y'

select T0.CardCode,T0.CardName,T0.DocDate,T0.DocDueDate,T0.DocTotal, 
T1.E_Mail, isnull(@emaill,'') ToEmailList, T0.DocEntry, T0.DocNum,
(select top(1) whscode from POR1 where DocEntry=T0.DocEntry) WhsCode
from OPOR T0
join OCRD T1 on T0.CardCode=T1.CardCode
where DocDate between @FromDate and @ToDate
and (DocStatus=@OpenOnly or @OpenOnly='')
and CANCELED='N'






